# -*- coding: utf-8 -*-
import requests
import re
import bs4
import traceback
from bs4 import BeautifulSoup

"""
未完待续
"""


def getHtmlText(url, code='utf-8'):
    """
    定向爬取股票数据
    :param url:
    :return:
    """
    try:
        r = requests.get(url)
        r.raise_for_status()
        r.encoding = code
        return r.text
    except:
        return ""


def getStockList(lst, stockURL):
    """
    获取股票列表
    :param lst:
    :param stockURL:
    :return:
    """
    html = getHtmlText(stockURL, 'GB2312')
    stock_name_list = re.findall(r'\"short_name\"\:\".*?"', html)
    stock_code_list = re.findall(r'\"stock_code\"\:\"\d{6}\"', html)
    for i in range(len(stock_name_list)):
        stock_name = stock_name_list[i]
        stock_code = stock_code_list[i]
        slist.append({stock_name: stock_code})



def getStockInfo(lst, stockURL, fpath):
    """
    获取每个个股的信息，并存到本地文件
    :param lst:
    :param stockURL:
    :param fpath:
    :return:
    """
    return ""


if __name__ == '__main__':
    stock_list_url = "http://data.eastmoney.com/notices/"
    stock_info_url = "http://quote.eastmoney.com/"
    output_file = "~/Desktop/stock.txt"
    slist = []  # 股票信息列表

    getStockList(slist, stock_list_url)
    getStockInfo(slist, stock_info_url, output_file)



